package com.ruoyi.web.controller.byManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.ByBs;
import com.ruoyi.system.domain.ByOrder;
import com.ruoyi.system.service.IByBsService;
import com.ruoyi.system.service.IByOrderService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 *
 * @author ruoyi
 * @date 2023-05-29
 */
@RestController
@RequestMapping("/by/order")
public class ByOrderController extends BaseController
{
    @Autowired
    private IByOrderService byOrderService;

    @Autowired
    private IByBsService bsService;


    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('by:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(ByOrder byOrder)
    {
        startPage();
        List<ByOrder> list = byOrderService.selectByOrderList(byOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('by:order:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ByOrder byOrder)
    {
        List<ByOrder> list = byOrderService.selectByOrderList(byOrder);
        ExcelUtil<ByOrder> util = new ExcelUtil<ByOrder>(ByOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('by:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(byOrderService.selectByOrderById(id));
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('by:order:getOrderInfo')")
    @PostMapping("getOrderInfo")
    public AjaxResult getOrderInfo(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String id = json.getString("id");
        ByOrder order = byOrderService.selectByOrderById(Long.parseLong(id));
        String bsid = order.getBsId();
        ByBs bs = bsService.selectByBsById(Long.parseLong(bsid));
        Map map = new HashMap();
        map.put("order",order);
        map.put("bs",bs);

        return success(map);
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('by:order:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ByOrder byOrder)
    {
        return toAjax(byOrderService.insertByOrder(byOrder));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('by:order:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ByOrder byOrder)
    {
        return toAjax(byOrderService.updateByOrder(byOrder));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('by:order:remove')")
        @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(byOrderService.deleteByOrderByIds(ids));
    }
}
